1   /*
2    * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.nio.cs.ext;
27  
28  import java.nio.charset.Charset;
29  import java.nio.charset.CharsetDecoder;
30  import java.nio.charset.CharsetEncoder;
31  import sun.nio.cs.HistoricallyNamedCharset;
32  import java.util.Arrays;
33  import static sun.nio.cs.CharsetMapping.*;
34  
35  public class Big5_Solaris extends Charset implements HistoricallyNamedCharset
36  {
37      public Big5_Solaris() {
38          super("x-Big5-Solaris", ExtendedCharsets.aliasesFor("x-Big5-Solaris"));
39      }
40  
41      public String historicalName() {
42          return "Big5_Solaris";
43      }
44  
45      public boolean contains(Charset cs) {
46          return ((cs.name().equals("US-ASCII"))
47                  || (cs instanceof Big5)
48                  || (cs instanceof Big5_Solaris));
49      }
50  
51      public CharsetDecoder newDecoder() {
52          initb2c();
53          return new  DoubleByte.Decoder(this, b2c, b2cSB, 0x40, 0xfe);
54      }
55  
56      public CharsetEncoder newEncoder() {
57          initc2b();
58          return new DoubleByte.Encoder(this, c2b, c2bIndex);
59      }
60  
61      static char[][] b2c;
62      static char[] b2cSB;
63      private static volatile boolean b2cInitialized = false;
64  
65      static void initb2c() {
66          if (b2cInitialized)
67              return;
68          synchronized (Big5_Solaris.class) {
69              if (b2cInitialized)
70                  return;
71              Big5.initb2c();
72              b2c = Big5.b2c.clone();
73              // Big5 Solaris implementation has 7 additional mappings
74              int[] sol = new int[] {
75                  0xF9D6, 0x7881,
76                  0xF9D7, 0x92B9,
77                  0xF9D8, 0x88CF,
78                  0xF9D9, 0x58BB,
79                  0xF9DA, 0x6052,
80                  0xF9DB, 0x7CA7,
81                  0xF9DC, 0x5AFA };
82              if (b2c[0xf9] == DoubleByte.B2C_UNMAPPABLE) {
83                  b2c[0xf9] = new char[0xfe - 0x40 + 1];
84                  Arrays.fill(b2c[0xf9], UNMAPPABLE_DECODING);
85              }
86  
87              for (int i = 0; i < sol.length;) {
88                  b2c[0xf9][sol[i++] & 0xff - 0x40] = (char)sol[i++];
89              }
90              b2cSB = Big5.b2cSB;
91              b2cInitialized = true;
92          }
93      }
94  
95      static char[] c2b;
96      static char[] c2bIndex;
97      private static volatile boolean c2bInitialized = false;
98  
99      static void initc2b() {
100         if (c2bInitialized)
101             return;
102         synchronized (Big5_Solaris.class) {
103             if (c2bInitialized)
104                 return;
105             Big5.initc2b();
106             c2b = Big5.c2b.clone();
107             c2bIndex = Big5.c2bIndex.clone();
108             int[] sol = new int[] {
109                 0x7881, 0xF9D6,
110                 0x92B9, 0xF9D7,
111                 0x88CF, 0xF9D8,
112                 0x58BB, 0xF9D9,
113                 0x6052, 0xF9DA,
114                 0x7CA7, 0xF9DB,
115                 0x5AFA, 0xF9DC };
116 
117             for (int i = 0; i < sol.length;) {
118                 int c = sol[i++];
119                 // no need to check c2bIndex[c >>8], we know it points
120                 // to the appropriate place.
121                 c2b[c2bIndex[c >> 8] + (c & 0xff)] = (char)sol[i++];
122             }
123             c2bInitialized = true;
124         }
125     }
126 }